﻿using CPPEI.Coolzon.Designer.Event.UI;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for EventView.xaml
    /// </summary>
    public partial class EventView : View
    {
        private Coolzon.UI.Part _selectedPart;
        public event SceneDirtiedEvent Dirtied;
        public EventView()
        {
            InitializeComponent();
        }

        public void SelectPartChanged(Coolzon.UI.Part partInstance)
        {
            _ctlGrid.RowDefinitions.Clear();
            _ctlGrid.Children.Clear();
            if (partInstance != null && UIContext.Current.CurrentEditedNode != null && (UIContext.Current.CurrentEditedNode.NodeStatus == NodeStatus.CheckOut || UIContext.Current.CurrentEditedNode.NodeStatus == NodeStatus.Add))
            {
                _selectedPart = partInstance;
                XEvent[] events = XEventManager.GetEvents(partInstance.GetType());
                // 1.显示组件的事件编辑器，展现形式和属性编辑一样。让用户输入事件执行js函数名称
                // 2.输入的值通过Metadata.BaseNode.XSetScriptEvent(string,string)来更新。
                // 3.Metadata.NodeBase 通过Part.XGetMetadata()来获取
                if (events != null && events.Length > 0)
                {
                    int rowIndex = 0;
                    foreach (var item in events)
                    {
                        _ctlGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                        TextBlock tb = new TextBlock();
                        tb.Text = item.Name;
                        tb.Margin = new Thickness(3, 6, 3, 3);
                        TextBox tbox = new TextBox();
                        tbox.Margin = new Thickness(3);

                        List<Metadata.ScriptEvent> scriptEvents = _selectedPart.XGetMetadata().ScriptEvents;
                        if (scriptEvents != null && scriptEvents.Count != 0)
                        {
                            for (int index = 0; index < scriptEvents.Count; index++)
                            {
                                if (string.Equals(scriptEvents[index].Source, item.Name, System.StringComparison.CurrentCultureIgnoreCase))
                                {
                                    tbox.Text = scriptEvents[index].Script;
                                    break;
                                }
                            }
                        }
                        tbox.LostFocus += Tbox_LostFocus;
                        tbox.Tag = item;
                        Grid.SetColumn(tb, 0);
                        Grid.SetColumn(tbox, 1);
                        Grid.SetRow(tb, rowIndex);
                        Grid.SetRow(tbox, rowIndex);
                        rowIndex++;
                        _ctlGrid.Children.Add(tb);
                        _ctlGrid.Children.Add(tbox);
                    }
                }
            }
        }

        public void Clear()
        {
            _ctlGrid.Children.Clear();
        }

        private void Tbox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (_selectedPart != null)
            {
                //属性变动时,调用Dirtied
                if (Dirtied != null)
                {
                    Dirtied();
                }
                Metadata.NodeBase node = _selectedPart.XGetMetadata();
                TextBox tb = sender as TextBox;
                XEvent xEvent = tb.Tag as XEvent;
                node.XSetScriptEvent(xEvent.Name, tb.Text.Trim());
            }
        }
    }
}
